//
//  ViewController.m
//  09-TableViewText
//
//  Created by dllo on 16/7/26.
//  Copyright © 2016年 YMJ. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *nameArray;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.tableView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain];
    [self.view addSubview:_tableView];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    self.navigationController.navigationBar.translucent = NO;
    _tableView.contentInset = UIEdgeInsetsMake(0, 0, 64, 0);
    [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"1"];
    NSArray *arr = @[@"唐益梦", @"张合尉", @"高雅馨", @"符淋淋", @"孙遥", @"李毅然", @"于瀚", @"沙长健", @"卢思彤", @"徐昊", @"曹冠雄", @"陈丹明", @"罗浩", @"肖扬", @"王道林", @"宫雪", @"李成", @"冀永金", @"姜浩", @"罗鹏飞", @"李昊林", @"郭宝", @"马峰", @"刘坦奇", @"殷明静", @"邓凯", @"徐康", @"王鍠", @"韩旭", @"杨睿", @"张信宝", @"姜程程", @"包铭礼", @"杜江", @"苗钧豪"];
    self.nameArray = [NSMutableArray arrayWithArray:arr];
    self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
#pragma mark - tableViewDelegate 
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    // 多选样式
    return UITableViewCellEditingStyleDelete;
}
#pragma mark - tableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _nameArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"1" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor brownColor];
    cell.textLabel.text = _nameArray[indexPath.row];
    return cell;
}
- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
    [super setEditing:editing animated:animated];
    [_tableView setEditing:editing animated:animated];
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
}
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
    
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        NSLog(@"删除");
        [_nameArray removeObject:_nameArray[indexPath.row]];
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
        
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        NSLog(@"添加");
        [_nameArray insertObject:@"大保健" atIndex:indexPath.row];
        [tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
    }
}
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row % 2 == 0) {
        return @"可以删除";
    } else {
        return @"不可删除";
    }
}
- (nullable NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewRowAction *rowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"收藏" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
        
        
        
    }];
    UITableViewRowAction *rowAction1 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"置顶" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
        NSString *name = [[_nameArray objectAtIndex:indexPath.row] copy];
        [_nameArray removeObjectAtIndex:indexPath.row];
        [_nameArray insertObject:name atIndex:0];
//        [tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationMiddle];
        [tableView reloadData];
        
    }];
    UITableViewRowAction *rowAction2 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"删除" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
        [_nameArray removeObjectAtIndex:indexPath.row];
//        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationMiddle];
        
        [tableView reloadData];
    }];
    rowAction2.backgroundColor = [UIColor purpleColor];
    rowAction.backgroundColor = [UIColor redColor];
    rowAction1.backgroundColor = [UIColor blueColor];
    return @[rowAction,rowAction1,rowAction2];
}
// Data manipulation - reorder / moving support

- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath {
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
